// pages/score/index.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		courseId: 0,
		teacherId: 0,
		schoolId: 0,
		hasSave: false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		if (options.courseId) {
			this.setData({
				courseId: options.courseId,
				parentRemarkId: options.parentRemarkId
			})
		} else if (options.teacherId) {
			this.setData({
				teacherId: options.teacherId,
				parentRemarkId: options.parentRemarkId

			})
		} else if (options.schoolId) {
			this.setData({
				schoolId: options.schoolId,
				parentRemarkId: options.parentRemarkId

			})
		};
	},
	textcontent: '', //评论内容
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},

	//评论内容
	textareafn: function(e) {
		this.textcontent = e.detail.value
	},
	//保存评论
	saveFn: function() {
		this.setData({
			hasSave: true
		})
		var that = this;
		var loginInfo = loginFn()
		var url = ''
		loginInfo.remark = this.textcontent
		loginInfo.parentRemarkId = this.data.parentRemarkId
		if (this.data.courseId !== 0) {
			loginInfo.courseId = this.data.courseId
			url = urls.saveCourse
		} else if (this.data.teacherId !== 0) {
			loginInfo.teacherId = this.data.teacherId
			url = urls.saveTeacherscore
		} else if (this.data.schoolId !== 0) {
			loginInfo.schoolId = this.data.schoolId
			url = urls.saveSchool
		}
		wx.request({
			url: url,
			method: "POST",
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					wx.showToast({
						title: '评论成功',
					})
					setTimeout(function() {
						wx.navigateBack({
							delta: 1
						})
					}, 2000)

				} else {
					wx.showToast({
						title: '评论失败',
					})
					that.setData({
						hasSave: false
					})
				};
			}
		})
	},
	//取消评论
	cancelFn: function() {
		wx.navigateBack({
			delta: 1
		})
	}

})
